import 'dart:math';
import 'package:flutter/material.dart';

class HeroPage extends StatefulWidget {
  const HeroPage({Key? key}) : super(key: key);

  @override
  _HeroPageState createState() => _HeroPageState();
}

class _HeroPageState extends State<HeroPage>
    with SingleTickerProviderStateMixin {
  late AnimationController _controller =
  AnimationController(vsync: this, duration: Duration(seconds: 1));
  List<SnowFlake> _snowFlakes = List.generate(100, (index) => SnowFlake());

  @override
  void initState() {
    super.initState();
    _controller.repeat();
  }

  @override
  void dispose() {
    super.dispose();
    _controller.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: SafeArea(
          child: Column(children: [
            Hero(
              tag: 'flutterLogo',
              child: FlutterLogo(size: 200.0),
            ),
            Container(
                width: double.infinity,
                height: 500,
                decoration: BoxDecoration(
                  gradient: LinearGradient(
                      colors: [Colors.blue, Colors.lightBlue, Colors.white],
                      begin: Alignment.topCenter,
                      end: Alignment.bottomCenter),
                ),
                child: AnimatedBuilder(
                  animation: _controller,
                  builder: (BuildContext context, Widget? child) {
                    _snowFlakes.forEach((snowFlake) {
                      snowFlake.fall();
                    });
                    return CustomPaint(
                      painter: myPainter(_snowFlakes),
                    );
                  },
                ))
          ]),
        ));
  }
}

class myPainter extends CustomPainter {
  List<SnowFlake> _snowFlakes;

  myPainter(this._snowFlakes);

  @override
  void paint(Canvas canvas, Size size) {
    final Paint paint = Paint()..color = Colors.white;
    // canvas.drawCircle(Offset(size.width/2,size.height/2), 20, Paint());
    canvas.drawCircle(size.center(Offset.zero), 60, paint);
    canvas.drawOval(
        Rect.fromCenter(
            center: size.center(Offset(0, 170)), width: 200, height: 250),
        paint);
    _snowFlakes.forEach((snowFlake) {
      canvas.drawCircle(
          Offset(snowFlake.x, snowFlake.y), snowFlake.radius, paint);
    });
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    return true;
  }

  @override
  bool shouldRebuildSemantics(CustomPainter oldDelegate) {
    return true;
  }
}

class SnowFlake {
  double x = Random().nextDouble() * 380;
  double y = Random().nextDouble() * 500;
  double radius = Random().nextDouble() * 2 + 2;
  double velocity = Random().nextDouble() * 4 + 2;
  fall() {
    y += velocity;
    if (y > 500) {
      y = 0;
      x = Random().nextDouble() * 380;
      radius = Random().nextDouble() * 2 + 2;
      velocity = Random().nextDouble() * 4 + 2;
    }
  }
}
